package cn.xiuxius.embedded;

import com.intellij.ide.IconProvider;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class EasySTCIcons extends IconProvider {
    private static final Icon rawRunIcon = IconLoader.getIcon("/icons/logo.png", EasySTCIcons.class);
    public static final Icon RUN_ICON = new ScaledIcon(rawRunIcon, 22, 22);  // 你想要的大小
    public static final Icon LOGO_ICON = new ScaledIcon(rawRunIcon, 20, 20);


    @Override
    public @Nullable Icon getIcon(@NotNull PsiElement psiElement, int flags) {
        // 先判断是不是文件类型的 PSI 元素
        if (psiElement instanceof PsiFile file) {
            VirtualFile vFile = file.getVirtualFile();
            if (vFile != null && "easy-stc.yml".equals(vFile.getName())) {
                return LOGO_ICON;
            }
        }
        return null;
    }

    public static class ScaledIcon implements Icon {
        private final Icon delegate;
        private final int width;
        private final int height;

        public ScaledIcon(Icon delegate, int width, int height) {
            this.delegate = delegate;
            this.width = width;
            this.height = height;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.translate(x, y);
            g2.scale((double) width / delegate.getIconWidth(), (double) height / delegate.getIconHeight());
            delegate.paintIcon(c, g2, 0, 0);
            g2.dispose();
        }

        @Override
        public int getIconWidth() {
            return width;
        }

        @Override
        public int getIconHeight() {
            return height;
        }
    }
}
